package com.fdhwws.web.dao.impl;

import com.fdh.myjdbcutil.MyJdbcUtil;
import com.fdhwws.web.dao.ChatRoomMsgDao;
import com.fdhwws.web.domain.ChatRoomMsg;

import java.util.List;

public class ChatRoomMsgDaoImpl implements ChatRoomMsgDao {
    @Override
    public ChatRoomMsg selectById(Integer id) {
        return MyJdbcUtil.getInstance()
                .selectSql("SELECT * FROM chat_room_msg WHERE id =?",
                        new Object[]{id},
                        ChatRoomMsg.class);
    }

    @Override
    public List<ChatRoomMsg> selectAll() {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM chat_room_msg",
                        ChatRoomMsg.class);
    }

    @Override
    public int insert(ChatRoomMsg entity) {
        return MyJdbcUtil.getInstance()
                .insertSql_upgrade(entity);
    }

    @Override
    public int update(ChatRoomMsg entity) {
        return MyJdbcUtil.getInstance()
                .updateSql_upgrade(entity);
    }

    @Override
    public int delete(Integer id) {
        return MyJdbcUtil.getInstance()
                .updateSql("DELETE FROM chat_room_msg WHERE id =?", new Object[]{id});
    }

    @Override
    public List<ChatRoomMsg> selectListByRoomId(Integer roomId) {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM chat_room_msg WHERE room_id =? AND valid = 1",
                        new Object[]{roomId},
                        ChatRoomMsg.class);
    }

    @Override
    public List<ChatRoomMsg> selectListByUserId(Integer userId) {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM chat_room_msg WHERE user_id =?",
                        new Object[]{userId},
                        ChatRoomMsg.class);
    }

    @Override
    public List<ChatRoomMsg> selectListByUsername(String username) {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM chat_room_msg WHERE username LIKE %?%",
                        new Object[]{username},
                        ChatRoomMsg.class);
    }
}
